package lero.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lero.pojo.Admin;
import lero.pojo.Author;
import lero.vo.AuthorVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/author")
public class AuthorController extends BaseController{

    //定义一个全局的日志记录器，通过LoggerFactory获取
    private static final Logger logger = LoggerFactory.getLogger(AuthorController.class);

    /**
     *功能描述:新增权限
     * @date 2018/7/19
     * @param author ：pojo实体
     * @return java.util.HashMap ：返回map数据
     */
    @RequestMapping("/insert.do")
    @ResponseBody
    public HashMap insert(@ModelAttribute("author") Author author){
        authorServiceI.insert(author);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }


    @RequestMapping("/select.do")
    @ResponseBody
    public  PageInfo<Author> select(@RequestBody AuthorVo vo){
        PageHelper.startPage(vo.getCurrentPage(),vo.getRows());
        List<Author> authors = authorServiceI.select(vo);
        PageInfo<Author> info = new PageInfo<>(authors);
        return info;
    }

    /**
     *功能描述:删除单条数据
     * @date 2018/7/19
     * @param id
     * @return java.util.HashMap ：返回map数据
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public  HashMap delete(@RequestParam("id") int id){
        authorServiceI.delete(id);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }

    /**
     *功能描述:批量删除
     * @date 2018/7/19
     * @param ids  批量删除数据的id组成的数组
     * @return java.util.HashMap ：返回map数据
     */
    @RequestMapping("/deleteMany.do")
    @ResponseBody
    public  HashMap deleteMany(@RequestParam("ids[]") int[] ids){
        authorServiceI.deleteMany(ids);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }


    /**
     *功能描述:
     * @date 2018/7/10
     * @param id : 查询权限的id
     * @return lero.pojo.Author
     */
    @RequestMapping("/selectOne.do")
    @ResponseBody
    public  Author selectOne(@RequestParam("id") int id){
        Author author = authorServiceI.selectOne(id);
        return author;
    }


    @RequestMapping(value="/update.do", method = RequestMethod.POST)
    @ResponseBody
    public  HashMap update(@ModelAttribute("author") Author author){
        authorServiceI.update(author);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }

    @RequestMapping(value="/getTree.do")
    @ResponseBody
    public  List<Map<String,Object>> getTree(){
        List<Map<String,Object>> tree = authorServiceI.getTree();
        return tree;
    }


    @RequestMapping(value="/selectAuthorByCurrentRole.do")
    @ResponseBody
    public  HashMap selectAuthorByCurrentRole(@RequestParam("roleId") int roleId){
        List<Map<String,Object>> tree = authorServiceI.selectAuthorByCurrentRole(roleId);
        HashMap map = new HashMap();
        map.put("code",0);
        map.put("msg",lero.common.Constant.SUCCESS.toString());
        HashMap data = new HashMap();
        data.put("trees",tree);
        map.put("data",data);
        return map;
    }


    @RequestMapping(value="/saveAuthorIntoCurrentRole.do")
    @ResponseBody
    public  HashMap saveAuthorIntoCurrentRole(@RequestParam("ids") String ids,@RequestParam("id") int id){
        //当前角色没有分配任何权限
        if(StringUtils.isEmpty(ids)){
           authorServiceI.deleteAuthorByCurrentRole(id);
        }else{
            String[] split = ids.split(",");
            int[] ints = new int[split.length];
            for (int i=0;i<ints.length;i++) {
                ints[i]= Integer.parseInt(split[i]);
            }
            authorServiceI.saveAuthorIntoCurrentRole(ints,id);
        }

        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }

    @RequestMapping(value="/selectCurentAdminMenuAuthor.do")
    @ResponseBody
    public  List<Map<String, Object>> selectCurentAdminMenuAuthor(HttpSession session){
        Admin loginUser = (Admin)session.getAttribute("loginUser");
        HashMap map = new HashMap();
        if(loginUser!=null){
            return authorServiceI.selectCurentAdminMenuAuthor(loginUser);
        }else{
            return null;
        }
    }

}
